<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Edit_repair
 *
 * @author peerawatj20
 */
class view_repair extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('repair_model');
        $this->load->model('truck/truck_model');
        $this->load->model('stock/stock_model');
        $this->load->model('catalog');
    }

    public function index() {
        $id = $this->input->get('id');
        $module = $this->input->get('module');
        $this->data['module'] = $module;
        $this->_set_form($id);
        $this->load->view('repair/view_repair', $this->data);
    }

    private function _set_form($id) {
        $utility = new Config_utility();
        $obj = $this->repair_model->get_repair_by_id($id);
        $this->data['lbl_created_date'] = '<h5>' . $utility->convert_date($obj[0]->created_date, $utility->DAY) . "  " .
                $utility->month_to_string($utility->convert_date($obj[0]->created_date, $utility->MONTH)) . "  " .
                $utility->convert_date($obj[0]->created_date + 543, $utility->YEAR) . '</h5>';
        $this->data['lbl_licen_code'] = '<h5>' . $utility->check_str($obj[0]->license_plate_code) . '</h5>';
        $this->data['lbl_driver_name'] = '<h5>' . $obj[0]->prefix_name . $utility->check_str($obj[0]->first_name . " " . $obj[0]->last_name) . '</h5>';
        $this->data['lbl_repair_type'] = '<h5>' . $utility->check_str($obj[0]->status_name) . '</h5>';
        $this->data['lbl_detail'] = form_textarea(array('name' => '', 'rows' => '5'), $obj[0]->repair_detail, 'class="form-control" style="resize: none;" readonly');
        $this->_set_table($id, $obj);
    }

    private function _set_table($id) {
        $utility = new Config_utility();
        $obj = $this->repair_model->get_order_by_id($id);
        $total_all = 0;
        $count = 0;

        for ($i = 0; $i < count($obj); $i++) {

            $row = '<tr id="tr_row' . $i . '"> 
                    <td> 
                        ' . ($i + 1) . ' 
                    </td> 
                    <td> 
                         <div id="lblMainType' . $i . '" style="height:22px;">' . $obj[$i]->main_type_name . '</div> 
                    </td> 
                    <td> 
                         <div id="lblSubType' . $i . '" style="height:22px;">' . $obj[$i]->sub_type_name . '</div> 
                    </td> 
                    <td> 
                         <div id="lblBrand' . $i . '" style="height:22px;">' . $obj[$i]->brand_name . '</div> 
                    </td> 
                    <td> 
                        <div id="lblAutoPart' . $i . '" style="height:22px;">' . $obj[$i]->stock_name . '</div> 
                    </td> 
                    <td> 
                        <div id="lblQty' . $i . '" style="height:22px;" >' . $utility->format_number($obj[$i]->qty) . ' ' . $obj[$i]->classifire . '</div> 
                    </td> 
                    <td> 
                        <div id="lblTotalPrice' . $i . '" style="height:22px;">' . $utility->format_number($obj[$i]->total_price) . ' บาท</div> 
                    </td> 
                </tr>';
            $total_all = $total_all + $obj[$i]->total_price;

            $this->data['row_table'][$i] = $row;
            $count++;
        }
        $total_price = '<tr><td colspan="6" class="text-right"><strong>ราคารวมทั้งหมด</strong></td><td><strong>' . $utility->format_number($total_all) . ' บาท</strong></td></tr>';
        $this->data['total_price'] = $total_price;
        $this->data['row_total'] = $count;
        $this->data['total_all'] = $total_all;
    }

}
